//
//  WBEmoticonCell.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/20.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBEmoticonCell.h"
#import <Masonry.h>
#import "WBEmoticonModel.h"
#import "NSString+Emoji.h"
#import "WBEmoticonTool.h"
#import "UIImage+WBImage.h"


@interface  WBEmoticonCell()

@property (nonatomic , strong) UILabel *numLabel;

@property (nonatomic , strong) NSMutableArray<UIButton *> *buttonArray;


@end

@implementation WBEmoticonCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self setupUI];
        
    }
    
    return self;
    
}

- (NSMutableArray<UIButton *> *)buttonArray
{
    if(!_buttonArray)
    {
        _buttonArray = [NSMutableArray array];
    }
    
    return _buttonArray;
    
}

- (UILabel *)numLabel
{
    if(!_numLabel)
    {
        _numLabel = [[UILabel alloc] init];
        
        _numLabel.preferredMaxLayoutWidth = kScreenW - 20;
        _numLabel.numberOfLines = 0;
        _numLabel.font = [UIFont systemFontOfSize:30];
        _numLabel.textColor = [UIColor whiteColor];
        
    }
    
    return _numLabel;
    
}

- (void)setIndex:(NSIndexPath *)index
{
    _index = index;
    
    _numLabel.text = [NSString stringWithFormat:@"第%zd组\n第%zd页",index.section,index.item];
    
}

#pragma mark - 重写set方法 给button赋值
- (void)setEmoticons:(NSArray<WBEmoticonModel *> *)emoticons
{
    _emoticons = emoticons;
    
    //遍历按钮 隐藏
    for (UIButton *btn in self.buttonArray) {
        
        btn.hidden = YES;
    }
    
    
    
    [emoticons enumerateObjectsUsingBlock:^(WBEmoticonModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        //获取btn
        UIButton *btn = self.buttonArray[idx];
        btn.hidden = NO;
        
        if(obj.isEmoji)
        {
            //给button赋值
            [btn setTitle:obj.code.emoji forState:UIControlStateNormal];
            [btn setImage:nil forState:UIControlStateNormal];
        }else
        {
            [btn setImage:[UIImage getImageWithModel:obj] forState:UIControlStateNormal];
            [btn setTitle:nil forState:UIControlStateNormal];
        }
        
    }];
    
}

- (void)setupUI
{
    self.backgroundColor = [UIColor randomColor];
    
    //添加控件
    [self addChildbutton];
    [self.contentView addSubview:self.numLabel];
    
    //设置约束
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.mas_equalTo(self.contentView);
    }];
    
}

//创建20个按钮
- (void)addChildbutton
{
    
    for(int i = 0 ; i < WBEmoticonsMaxCount ; i++)
    {
        //创建buttOn
        UIButton *btn = [[UIButton alloc] init];
        
        btn.backgroundColor = [UIColor randomColor];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:30];
        
        //添加到当前数组中
        [self.buttonArray addObject:btn];
        
        //添加到当前cell上面
        [self.contentView addSubview:btn];
        
    }
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat btnW = self.frame.size.width / WBEmoticonsMaxCol;
    CGFloat btnH = self.frame.size.height / WBEmoticonsMaxRow;
    
    [self.buttonArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        int col = idx % WBEmoticonsMaxCol;
        int row = (int)idx / WBEmoticonsMaxCol;
        
        obj.frame = CGRectMake(col * btnW, row * btnH, btnW, btnH);
        
    }];
    
}

@end
